package cn.lsnu.modules.check.controller;


import cn.lsnu.common.interfacetools.interfaceentity.Page;
import cn.lsnu.common.sys.entity.MenuEntity;
import cn.lsnu.common.sys.service.MenuService;
import cn.lsnu.modules.check.entity.CheckTaskEntity;
import cn.lsnu.modules.check.entity.InnerCheckEntity;
import cn.lsnu.modules.check.service.CheckTaskService;
import cn.lsnu.modules.check.service.InnerCheckService;
import cn.lsnu.utils.Global;
import cn.lsnu.utils.Guuid;
import cn.lsnu.utils.UploadTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping({"/app/innerchek","/check"})
public class InnerCheckController {

    @Autowired
    private InnerCheckService innerCheckService;

    @Autowired
    private CheckTaskService checkTaskService;

    @Autowired
    private MenuService menuService;

    @RequestMapping("/save")
    public
    @ResponseBody
    InnerCheckEntity save(InnerCheckEntity entity, HttpServletRequest request)throws Exception{
        String rpath = UploadTool.uploadImg("/upload/image",request);
        entity.setInnerImgUrl(rpath.trim().isEmpty()?null:rpath);
        CheckTaskEntity checkTaskEntity = new CheckTaskEntity();
        checkTaskEntity.setTaskTime(entity.getCheckDate()==null?new Date():entity.getCheckDate());
        if(entity.getInnerId() == null){
            entity.setInnerId(Guuid.uuid());
            innerCheckService.save(entity);
        }else{
            innerCheckService.update(entity);
        }
        checkTaskEntity.setCheckId(entity.getInnerId());
        checkTaskEntity.setGasId(entity.getWorkId());
        checkTaskEntity.setStatus("1");
        checkTaskService.updateStatus(checkTaskEntity);
        entity = entity == null?new InnerCheckEntity():entity;
        entity.setCode("200");
        entity.setMsg("提交成功");
        return entity;
    }

    @RequestMapping("/checkdetail")
    public
    @ResponseBody
    InnerCheckEntity checkDetail(InnerCheckEntity entity)throws Exception{
        entity = innerCheckService.getEntity(entity);
        entity.setCode("200");
        entity.setMsg("提交成功");
        return entity;
    }

    @RequestMapping("/inner/list")
    public String listInfos(Model model, HttpSession session, MenuEntity entity)throws Exception{
        String firstTitle = entity.getFirstTitle();
        //每个-list页面的id
        model.addAttribute("pageId",entity.getMenuId());
        //获取页面的所有信息
        String userCode = (String) session.getAttribute(Global.SESSION_ID);
        entity.setUserCode(userCode);
        entity = menuService.getPageMenu(entity);
        entity.setFirstTitle(firstTitle);
        entity.setSecondTitle("室内");
        model.addAttribute("pageMenuMsg",entity);
        return "webpage/modules/check/checkinner-list";
    }

    @RequestMapping("/inner/listjson")
    public @ResponseBody
        Page<InnerCheckEntity> listJson(InnerCheckEntity entity)throws  Exception{
        return  innerCheckService.findPage(entity);
    }

    @RequestMapping("/inner/detail")
    public String updMsg(String key,Model model)throws Exception{
        InnerCheckEntity  entity = innerCheckService.getInner(key);
        model.addAttribute("innercheck",entity);
        return "webpage/modules/check/checkinner-detail";
    }

}

